package com.jsmtr.rms.controller.DoZZCJ;

import com.jsmtr.rms.entity.*;
import com.jsmtr.rms.service.*;
import com.jsmtr.rms.utils.EnumTools;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Do/ZZCJInterview")
public class DoZZCJInterviewController {

    @Resource
    private IBaseEvaluationService baseEvaluationService;
    @Resource
    private IBaseExaminerService baseExaminerService;
    @Resource
    private IUtilService utilService;
    @Resource
    private IDoGXService doGXService;
    @Resource
    private IDoMSService msService;
    @Resource
    private IDoResumeService doResumeService;
    @Resource
    private IProjectService projectService;


    //[start] 面试管理

    //面试管理主界面
    @RequestMapping("/")
    public String Index(HttpServletRequest request, HttpServletResponse response, Model model) {
        try {
            model.addAttribute("path", request.getContextPath());
            String pid = request.getParameter("pid").trim();
            String fid = request.getParameter("fid").trim();
            String deptcode = request.getParameter("deptcode").trim();

            model.addAttribute("pid", pid);
            model.addAttribute("fid", fid);
            model.addAttribute("deptcode", deptcode);

            //名族
            model.addAttribute("minzu", utilService.optionList("B", "0"));
            //政治面貌
            model.addAttribute("zzmm", utilService.optionList("E", "0"));
            //最高学历
            model.addAttribute("education", utilService.optionList("F", "0"));
            //招聘地区
            model.addAttribute("regions", utilService.optionList("M", "0"));
            //招聘岗位
            List<ProjectInfoJobs> projectJobs=utilService.getProjectJobs(pid);
            model.addAttribute("jobs",projectJobs);

            //评价表
            List<BaseEvaluationTable> tables = baseEvaluationService.getTables();
            StringBuilder evaluation = new StringBuilder();
            for (BaseEvaluationTable table : tables) {
                evaluation.append("<option value='" + table.getId() + "'>" + table.getName() + "</option>");
            }
            model.addAttribute("evaluation", evaluation);

            //面试地址
            model.addAttribute("dz", utilService.getAddress());

            //面试注意事项
            String mszysx=utilService.getConfig("mszysx");
            model.addAttribute("mszysx",mszysx);

            //面试组
            List<BaseExaminerGroup> groups = baseExaminerService.getGroups();
            StringBuilder examiner = new StringBuilder();
            for (BaseExaminerGroup group : groups) {
                examiner.append("<option value='" + group.getId() + "'>" + group.getName() + "</option>");
            }
            model.addAttribute("examiner", examiner);

            List<BaseConfig> _configs = utilService.getConfigs("dxmb");
            StringBuilder str = new StringBuilder();
            for (BaseConfig _config : _configs) {
                str.append("<option value='" + _config.getCode() + "'>" + _config.getName() + "</option>");
            }
            model.addAttribute("dxmb", str);

            List<String> mstimesList = msService.getMsTimse(fid);
            StringBuilder str_mstimes = new StringBuilder();
            for (String  timeses : mstimesList) {
                str_mstimes.append("<option value='" + timeses + "'>" + timeses + "</option>");
            }
            model.addAttribute("mstimes", str_mstimes);

            List<String> qmsaddresssList = msService.getMsAddresss(fid);
            StringBuilder qmsaddresss = new StringBuilder();
            for (String  addess : qmsaddresssList) {
                qmsaddresss.append("<option value='" +addess + "'>" + addess + "</option>");
            }
            model.addAttribute("msaddress", qmsaddresss);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/ZZCampusSpring/Interview/index";
    }

   /*
   @SuppressWarnings("unchecked")
    @RequestMapping("/getusers")
    public void getusers(HttpServletRequest request, HttpServletResponse response, DUserResume user) {
        try {
            int perpage = Integer.parseInt(request.getParameter("perpage"));
            int page = Integer.parseInt(request.getParameter("page"));
            String flowid = request.getParameter("flowid").trim();

            Map<String, Object> _map = new HashMap<String, Object>();
            _map.put("agefrom", request.getParameter("agefrom").trim());
            _map.put("ageto", request.getParameter("ageto").trim());
            _map.put("heightfrom", request.getParameter("heightfrom").trim());
            _map.put("heightto", request.getParameter("heightto").trim());
            _map.put("flowid", flowid);
            _map.put("msresult", request.getParameter("msresult").trim());
            _map.put("notice", request.getParameter("notice").trim());
            _map.put("isconfirm", request.getParameter("isconfirm").trim());
            Map<String, Object> map = doGXService.getMSUsers(page, perpage, user, _map);
            StringBuilder result = new StringBuilder();
            double pageCount = (Double) map.get("pageCount");
            double allcount = (Double) map.get("count");

            if (pageCount != 0) {
                Object object = map.get("list");
                for (Object[] _user : (List<Object[]>) object) {
                    //r.id,s.id as userid,r.code,r.name,r.cardaddress,r.university,r.majorname,r.educationname,
                    //s.starttime,s.address,s.notice,s.point,s.result,r.fid,r.fqstate,r.jname,s.op7,s.scoringrules
                    //增加确认时间和确认状态 c.confirmtime,c.isconfirm
                    result.append("<tr><td><input type='checkbox' id='cb_" + _user[0] + "' value='" + _user[0] + "' /></td>");
                    result.append("<td>" + _user[2] + "</td>");
                    result.append("<td class='link'><a target='_blank' href='"
                            + request.getContextPath() + "/UserResume/ViewDResume?id="
                            + _user[0] + "'>" + _user[3]
                            + "</a></td>");
                    result.append("<td>" +_user[4] + "</td>");
                    result.append("<td>" + _user[5] + "</td>");
                    result.append("<td>" + _user[6] + "</td>");
                    result.append("<td>" + _user[7] + "</td>");
                    result.append("<td>" + (_user[8] == null ? "" : _user[8]) + "</td>");
                    result.append("<td>" + (_user[9] == null ? "" : _user[9]) + "</td>");
                    //改为确认时间和确认状态
                    result.append("<td>" + (_user[17] == null ? "" : _user[17]) + "</td>");
                    result.append("<td>" + (_user[18] == null ? "" : _user[18]) + "</td>");
                    *//*result.append("<td class='link'><a href=\"javascript:viewEvaluation('" + _user[1] + "');\">" + (_user[8] == null ? "" : "评价表") + "</a></td>");
                    result.append("<td class='link'><a href=\"javascript:viewExaminer('" + _user[1] + "');\">" + (_user[8] == null ? "" : "面试组") + "</a></td>");*//*
                    result.append("<td>" + (_user[10] == null ? "" : EnumTools.NoticeState.values()[((BigDecimal) _user[10]).intValue()]) + "</td>");
                    result.append("<td class='link'>" + (_user[11] == null ? "" : "<a href=\"javascript:viewResult('" + _user[1] + "','" + _user[3] + "');\">" + _user[11] + "</a>") + "</td>");
                    result.append("<td>" + (_user[12] == null ? "" : _user[12]) + "</td>");
                    result.append("<td>" + (_user[19] == null ? "" : _user[19]) + "</td>");
                    if (_user[16] == null || _user[16].equals("未公布")) {
                        result.append("<td class='true'><span>未公布</span></td>");
                    } else {
                        result.append("<td class='false'><span>已公布</span></td>");
                    }

                    if (_user[14].equals("已放弃") && _user[13].equals(flowid)) {
                        result.append("<td class='yfq'><span>已放弃</span></td></tr>");
                    } else {
                        if (_user[12] == null) {
                            result.append("<td class='true'><span>未完成</td></tr>");
                        } else {
                            if (_user[12].equals("未面试")) {
                                result.append("<td class='true'><span>未完成</td></tr>");
                            } else if (_user[12].equals("未通过")) {
                                result.append("<td class='false'><span>已完成</td></tr>");
                            } else {
                                if (_user[13].equals(flowid)) {
                                    result.append("<td class='true'><span>未完成</td></tr>");
                                } else {
                                    result.append("<td class='false'><span>已完成</td></tr>");
                                }
                            }
                        }
                    }
                }
            } else {
                result.append("<tr><td colspan='16'>未找到符合条件的数据</td></tr>");
            }
            result.append("<input type='hidden' id='pageCount' value='" + pageCount + "' />");
            result.append("<input type='hidden' id='allCount' value='" + allcount + "' />");
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("<tr><td colspan='16'>读取数据失败</td></tr><input type='hidden' id='pageCount' value='0' /><input type='hidden' id='allCount' value='0' />", response);
        }

    }
*/

    //region 20190225改为复杂查询返回MAP
    @RequestMapping("/getusers")
    public void getusers(HttpServletRequest request, HttpServletResponse response, DUserResume user) {
        try {
            int perpage = Integer.parseInt(request.getParameter("perpage"));
            int page = Integer.parseInt(request.getParameter("page"));
            String flowid = request.getParameter("flowid").trim();

            Map<String, Object> _map = new HashMap<String, Object>();
            _map.put("agefrom", request.getParameter("agefrom").trim());
            _map.put("ageto", request.getParameter("ageto").trim());
            _map.put("heightfrom", request.getParameter("heightfrom").trim());
            _map.put("heightto", request.getParameter("heightto").trim());
            _map.put("flowid", flowid);
            _map.put("msresult", request.getParameter("msresult").trim());
            _map.put("notice", request.getParameter("notice").trim());
            _map.put("isconfirm", request.getParameter("isconfirm").trim());
            _map.put("msplan", request.getParameter("msplan"));
            _map.put("msplantime", request.getParameter("msplantime"));
            _map.put("msaddress", request.getParameter("msaddress"));
            Map<String, Object> map = doGXService.getMSUsers(page, perpage, user, _map);
            StringBuilder result = new StringBuilder();
            double pageCount = (Double) map.get("pageCount");
            double allcount = (Double) map.get("count");

            if (pageCount != 0) {
                List<HashMap> object = (List<HashMap>)map.get("list");
                for (HashMap<String,Object> _user :  object) {
                    //r.id,s.id as userid,r.code,r.name,r.cardaddress,r.university,r.majorname,r.educationname,
                    //s.starttime,s.address,s.notice,s.point,s.result,r.fid,r.fqstate,r.jname,s.op7,s.scoringrules
                    //增加确认时间和确认状态 c.confirmtime,c.isconfirm
                    result.append("<tr><td><input type='checkbox' id='cb_" + _user.get("ID") + "' value='" + _user.get("ID") + "' /></td>");
                    result.append("<td class='link'><a target='_blank' href='"
                            + request.getContextPath() + "/UserResume/ViewDResume?id="
                            + _user.get("ID") + "'>" + _user.get("NAME")
                            + "</a></td>");
                    result.append("<td>" + _user.get("CARDNO") + "</td>");
                    result.append("<td>" + _user.get("UNIVERSITY") + "</td>");
                    result.append("<td>" + _user.get("MAJORNAME") + "</td>");
                    result.append("<td>" + _user.get("EDUCATIONNAME") + "</td>");
                    result.append("<td>" + (_user.get("STARTTIME") == null ? "" : _user.get("STARTTIME")) + "</td>");
                    result.append("<td>" + (_user.get("ADDRESS") == null ? "" : _user.get("ADDRESS")) + "</td>");
                    //改为确认时间和确认状态
                    result.append("<td>" + (_user.get("ISCONFIRM") == null ? "" : _user.get("ISCONFIRM")) + "</td>");
                    result.append("<td>" + (_user.get("CONFIRMTIME") == null ? "" : _user.get("CONFIRMTIME")) + "</td>");

                    result.append("<td>" + (_user.get("NOTICE") == null ? "" : EnumTools.NoticeState.values()[((BigDecimal) _user.get("NOTICE")).intValue()]) + "</td>");
                    result.append("<td class='link'>" + (_user.get("POINT") == null ? "" : "<a href=\"javascript:viewResult('" + _user.get("USERID") + "','" + _user.get("NAME") + "');\">" + _user.get("POINT") + "</a>") + "</td>");
                    result.append("<td>" + (_user.get("RESULT") == null ? "" : _user.get("RESULT"))+ "</td>");
                    result.append("<td>" + (_user.get("MSREMARK") == null ? "" : _user.get("MSREMARK")) + "</td>");
                    if (_user.get("OP7") == null || _user.get("OP7").equals("未公布")) {
                        result.append("<td></td>");
                        result.append("<td><input type='hidden' value='"+_user.get("MID")+"'>"+ (_user.get("MCONTENT") == null ? "" : _user.get("MCONTENT")) + "</td>");
                        result.append("<td class='true'><span>未公布</span></td>");
                    } else {
                        result.append("<td>"+ (_user.get("LOGINTIME")==null?"":_user.get("LOGINTIME").toString())+"</td>");
                        result.append("<td><input type='hidden' value='"+_user.get("MID")+"'>"+ (_user.get("MCONTENT") == null ? "" : _user.get("MCONTENT")) + "</td>");
                        result.append("<td class='false'><span>已公布</span></td>");
                    }

                    result.append("<td>");
                    if (_user.get("FQSTATE").equals("已放弃") && _user.get("FQFID").equals(flowid)) {
                       // result.append("<td class='yfq'><span>已放弃</span></td></tr>");
                        result.append("<span class='layui-badge layui-bg-gray' title='已放弃'>弃</span>");
                    } else {

                            if (_user.get("RESULT") == null) {
                               // result.append("<td class='true'><span>未完成</td></tr>");
                                result.append("<span class='layui-badge layui-bg-orange' title='未完成'>未</span>");
                            } else {
                                if (_user.get("RESULT").equals("未面试")) {
                                    //result.append("<td class='true'><span>未完成</td></tr>");
                                    result.append("<span class='layui-badge layui-bg-orange' title='未完成'>未</span>");
                                } else if (_user.get("RESULT").equals("未通过")||_user.get("RESULT").equals("不通过")||_user.get("RESULT").equals("不合格")||_user.get("RESULT").equals("未参加")) {
                                   // result.append("<td class='false'><span>已完成</td></tr>");
                                    result.append("<span class='layui-badge layui-bg-green' title='已完成'>完</span>");
                                } else {
                                    if (_user.get("FID").equals(flowid)) {
                                       // result.append("<td class='true'><span>未完成</td></tr>");
                                        result.append("<span class='layui-badge layui-bg-orange' title='未完成'>未</span>");
                                    } else {
                                       //result.append("<td class='false'><span>已完成</td></tr>");
                                        result.append("<span class='layui-badge layui-bg-green' title='已完成'>完</span>");
                                    }
                                }
                            }
                    }
                    if (_user.get("NRID") != null) {
                        result.append("<td class='ytj'><span title='" +  _user.get("NJNAME") + "'>已推荐</span></td></tr>");
                    }
                    result.append("</td>");
                    result.append("</tr>");
                }
            } else {
                result.append("<tr><td colspan='18'>未找到符合条件的数据</td></tr>");
            }
            result.append("<input type='hidden' id='pageCount' value='" + pageCount + "' />");
            result.append("<input type='hidden' id='allCount' value='" + allcount + "' />");
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("<tr><td colspan='18'>读取数据失败</td></tr><input type='hidden' id='pageCount' value='0' /><input type='hidden' id='allCount' value='0' />", response);
        }
    }
    //endregion

    //[end]

    //[start] 流程扭转到下一步

    @SuppressWarnings({"rawtypes", "unchecked"})
    @RequestMapping(value = "/NextAction", method = RequestMethod.POST)
    public void NextAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String[] ids = request.getParameterValues("id");//人员信息ID
            if (ids != null) {
                String pid = request.getParameter("pid").trim();//招聘方案ID
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码
                BaseUser usr = utilService.getSessionUser(request);
                if (usr.getUdept().equals(deptcode)) {
                    //流程是否结束
                    if (utilService.FlowIsDoing(fid)) {
                        List editlist = new ArrayList();
                        List<DUserResume> users = doGXService.getResumes(ids);
                        ProjectInfoFlow nFlow = doGXService.getNextFlow(pid, deptcode, fid);

                        for (DUserResume user : users) {
                            if (user.getFid().equals(fid) && user.getFqstate().equals("未放弃")) {
                                DoMSUser msuser = msService.getMsUser(fid, user.getId());
                                //面试环节全部人员均可进入下一步在政审环节统一发布
                              /*  if(user.getState().equals("已通过")||user.getState().equalsIgnoreCase("通过")||user.getState().equalsIgnoreCase("合格")||user.getState().equalsIgnoreCase("已合格")) {*/
                                    user.setFid(nFlow.getId());
                                    user.setPath(user.getPath() + nFlow.getId() + "/");
                                    editlist.add(user);
                            }
                        }

                        if (editlist.size() > 0) {
                            nFlow.setState(EnumTools.PlanFlowState.进行中.ordinal());
                            editlist.add(nFlow);

                            utilService.editList(editlist);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中的信息不能执行此操作！", "no", response);
                        }
                    } else {
                        utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "请选择人员信息！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //[end]
}
